//
//  MyDateVC.m
//  MovieDate
//
//  Created by 杨运涛 on 16/3/13.
//  Copyright © 2016年 MovieDate. All rights reserved.
//

#import "MyDateVC.h"
#import "MyDateDetailVC.h"

@interface MyDateVC ()
{
    int pIntPage;
}

@property(nonatomic, strong) NSMutableArray *pDataDicArr;

@property (weak, nonatomic) IBOutlet UILabel *lblMessage;

@end

@implementation MyDateVC

- (void)initInfo
{
    [super initInfo];
    pIntPage = 1;
    _pDataDicArr = [NSMutableArray array];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self selAddLeftBackButton];
    [self selDefaultNavigationBackground];
    [self selDefaultNavigationWithTitle:@"我的约会"];
    
    __weak typeof(self) weakSelf = self;
    self.pTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
    
        pIntPage = 1;
        [weakSelf requestDataDic];
    }];
    
    self.pTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf requestDataDic];
    }];
    
    [self.pTableView.mj_header beginRefreshing];
    [self.pTableView.mj_footer setHidden:YES];
    [self.pTableView setTableFooterView:[UIView new]];
}

///pop或者dismiss
- (void)selPopOrDismissViewController {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)requestDataDic
{
    __weak typeof(self) weakSelf = self;
    

    [TDNetwork requestMySuccessMovieList:MY_Success_MovieList page:pIntPage pageSize:10 complete:^(BOOL succeed, NSString *ret, NSDictionary *dictionary) {
        if (succeed) {
                [weakSelf.pDataDicArr removeAllObjects];
            NSDictionary *dict = [dictionary valueForKey:@"res"];
            if (!dict || [dict allKeys].count == 0) {
                return;
            }
            NSArray *arr = [dict valueForKey:@"movieList"];
            [_pDataDicArr addObjectsFromArray:arr];
            
            [weakSelf.pTableView reloadData];
            
            if (arr.count == 10) {
                pIntPage ++;
                [self.pTableView.mj_footer setHidden:NO];
            } else {
                [self.pTableView.mj_footer setHidden:YES];
            }
        } else {
        }
        
        if (_pDataDicArr.count == 0) {
            [_lblMessage setHidden:NO];
        } else {
            [_lblMessage setHidden:YES];
        }
        
        [self.pTableView.mj_footer endRefreshing];
        [self.pTableView.mj_header endRefreshing];
    }];
}


#pragma mark - UITableViewDataSource, UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _pDataDicArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"idfMyDateCell"];
    
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"idfMyDateCell"];
    }
    NSDictionary *dict = [_pDataDicArr objectAtIndex:indexPath.row];
    UIImageView *imgPic = (UIImageView *)[cell.contentView viewWithTag:1301];
    UIImageView *imgSex = (UIImageView *)[cell.contentView viewWithTag:1302];
    UILabel *lblName = (UILabel *)[cell.contentView viewWithTag:1101];
    UILabel *lblAge = (UILabel *)[cell.contentView viewWithTag:1102];
    UILabel *lblTime = (UILabel *)[cell.contentView viewWithTag:1103];
    UILabel *lblAddress = (UILabel *)[cell.contentView viewWithTag:1104];
    
    [imgPic sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", IP, [dict objectForKey:@"headImage"]]]];
    [imgSex setImage:[UIImage imageNamed:[[dict objectForKey:@"userSex"] intValue]==0?@"sex_girl":@"sex_boy"]];
    [lblName setText:[NSString stringWithFormat:@"%@", [dict objectForKey:@"nickname"]]];
    [lblAge setText:[NSString stringWithFormat:@"%@", [dict objectForKey:@"userAge"]]];
    [lblTime setText:[NSString stringWithFormat:@"%@", [dict objectForKey:@"releaseTime"]]];
    [lblAddress setText:[NSString stringWithFormat:@"%@", [dict objectForKey:@"meetAddress"]]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dict = [_pDataDicArr objectAtIndex:indexPath.row];
    [self performSegueWithIdentifier:@"idfMyDateDetailVC" sender:dict];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"idfMyDateDetailVC"]) {
        MyDateDetailVC *nextVC = segue.destinationViewController;
        nextVC.pDict = sender;
    }
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}


@end
